<?php
/**
 * User: sovy
 * Date: 2018/2/24
 * Describe: 工作流
 */
defined('IN_IA') or exit('Access Denied');

global $_W, $_GPC;
load()->func('tpl');

require "public.php";
//require "staff_common.php";

$op = $_GPC['op'];
$uid = empty($_W['member']['uid']) ? $_GPC['uid'] : $_W['member']['uid'];
if ($op == 'edit') {
    //当前操作ID
    $data['id'] = $_GPC['id'];
    //当前步骤
    $step = empty($_GPC['step']) ? 1 : $_GPC['step'];
    $curStaff = pdo_get('cwgl_staff_list', array('uid' => $uid, 'weid' => $_W['uniacid']));

    //增加或修改
    if ($_W['ispost']) {
        $data['step'] = $step;
        if ($data['id'] > 0) {
            $curWorkflow = pdo_get('cwgl_workflow', array('id' => $_GPC['id']));
            $notice_company = $curWorkflow['st1_company'];
        } else {
            $notice_company = $_GPC['st1_company'];
        }
        if ($step == 2) {
            $data['st1_proposer_staff'] = $_GPC['st1_proposer_staff'];
            $data['st1_staff_class'] = $_GPC['st1_staff_class'];
            $data['st1_company'] = $_GPC['st1_company'];
            $data['st1_baseinfo'] = $_GPC['st1_baseinfo'];
            $data['st1_item'] = $_GPC['st1_item'];
            $data['st1_notice'] = $_GPC['st1_notice'];
            $data['st1_material'] = $_GPC['st1_material'];
            $data['st1_to1'] = $_GPC['st1_to1'];
            $data['st1_to2'] = $_GPC['st1_to2'];
            $notice_uid[] = 2;
            $curStaffTemp = pdo_get('cwgl_staff_list', array('id' => $_GPC['st1_proposer_staff'], 'weid' => $_W['uniacid']));
            $notice_mame[] = $curStaffTemp['name'];
        } else if ($step == 3) {
            $data['st2_agree'] = $_GPC['st2_agree'];
            if ($_GPC['st2_agree'] == 0) {
                $data['step'] = -1;
            }
            $data['st2_note'] = $_GPC['st2_note'];
            $data['st2_create_time'] = date('Y-m-d H:s:i');
            if (!empty($curWorkflow['st1_to1'])) {
                $notice_uid[] = $curWorkflow['st1_to1'];
                $notice_mame[] = '施文雪';
            }
            if (!empty($curWorkflow['st1_to2'])) {
                $notice_uid[] = $curWorkflow['st1_to2'];
                //$curStaffTemp = pdo_get('cwgl_staff_list', array('uid' => $curWorkflow['st1_to2'], 'weid' => $_W['uniacid']));
                $notice_mame[] = '施文雪';
            }
        } else if ($step == 4) {
            $data['st3_action_staff'] = $_GPC['st3_action_staff'];
            $data['st3_schedule'] = $_GPC['st3_schedule'];
            $data['st3_notice'] = $_GPC['st3_notice'];
            $data['st3_material'] = $_GPC['st3_material'];
            $data['st3_over_date'] = $_GPC['st3_over_date'];
            $data['st3_create_time'] = date('Y-m-d H:s:i');
            $notice_uid[] = 13;
            $notice_mame[] = $curStaff['name'];
        } else if ($step == 5) {
            $data['st4_note'] = $_GPC['st4_note'];
            $data['st4_create_time'] = date('Y-m-d H:s:i');
        }

        if ($data['id'] > 0) {
            $notice_work_id = $data['id'];
            unset($data['id']);
            pdo_update("cwgl_workflow", $data, array('id' => $_GPC['id']));
        } else {
            $data['st1_create_time'] = date('Y-m-d H:s:i');
            pdo_insert("cwgl_workflow", $data);
            //插入id
            $notice_work_id = pdo_insertid();

        }
        //消息提醒
        if (!empty($notice_uid)) {
            require_once('weixin.class.php');
            $uniacid = $_W['uniacid'];
            $url = $_W['siteroot'] . "app/index.php?i=".$_GPC['i']."&c=".$_GPC['c']."&m=".$_GPC['m']."&do=staff_workflow&op=edit&id=".$notice_work_id;
            //获取公众号配置信息
            $srdb = pdo_get('account_wechats', array('uniacid' => $uniacid));
            $appid = $srdb['key'];
            $appsecret = $srdb['secret'];
            $access_token_odl = $srdb['access_token'];
            $access_token = moban($appid, $appsecret, $access_token_odl, $uniacid);
            for ($i = 0; $i < count($notice_uid); $i++) {
                $mb_config = pdo_get('cwgl_config', array('weid' => $_W['uniacid']));
                //获取openid
                $user_openid = pdo_get('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'uid' => $notice_uid[$i]));
                $first = '您有新的工作流待办事项';

                $template = array(
                    'touser' => trim($user_openid['openid']),
                    'template_id' => trim($mb_config['mbid6']),    //模板的id
                    'url' => $url,
                    'topcolor' => "#FF0000",
                    'data' => array(
                        'first' => array('value' => urlencode($first), 'color' => "#00008B"),
                        'keyword1' => array('value' => urlencode('业务申请'), 'color' => "#00008B"),    //业务申请keyword1
                        'keyword2' => array('value' => urlencode($notice_company), 'color' => '#00008B'),        //公司名称keyword2
                        'keyword3' => array('value' => urlencode($notice_mame[$i]), 'color' => '#00008B'),        //转交人keyword3
                        'keyword4' => array('value' => urlencode(date('Y-m-d')), 'color' => '#00008B'),        //办理时间
                        'remark' => array('value' => urlencode("如有问题请尽快联系管理员！"), 'color' => '#00008B'),
                    )
                );
                $dataTemp = urldecode(json_encode($template));
                $send_result = send_template_message($dataTemp, $access_token);
            }
        }
        message('保存成功', $this->createMobileUrl('staff_workflow', array('op' => 'list')), 'success');

    } else {
        $curWorkflow = pdo_get('cwgl_workflow', array('id' => $data['id']));
        if (!empty($curWorkflow)) {
            $step = $curWorkflow['step'];
        }

        if ($step > 1) {
            if (empty($curWorkflow)) {
                message("工作流不存在", $this->createMobileUrl('staff_workflow', array('op' => 'list')), "error");
            }
            //第二步权限
            if ($step == 2) {
                if ($curStaff['name'] != '施文雪') {
                    message("您没有该权限", $this->createMobileUrl('staff_workflow', array('op' => 'list')), "error");
                }
            } //第三步权限
            else if ($step == 3) {
                if (!empty($curWorkflow['st3_action_staff'])) {
                    message("该工作流已被其他员工操作", $this->createMobileUrl('staff_workflow', array('op' => 'list')), "error");
                }
                if ($curStaff['id'] != $curWorkflow['st1_to1'] && $curStaff['id'] != $curWorkflow['st1_to2']) {
                    message("您没有该权限", $this->createMobileUrl('staff_workflow', array('op' => 'list')), "error");
                }
            } //第四步权限
            else if ($step == 4) {
                if ($curStaff['name'] != '施妙青') {
                    message("您没有该权限", $this->createMobileUrl('staff_workflow', array('op' => 'list')), "error");
                }
            }

        }
        $staffs = pdo_fetchall("select * from " . tablename('cwgl_staff_list') . " where weid=" . $_W['uniacid']);
        //当前部门
        if ($step == 1) {
            //当前部门
            $category = pdo_get('cwgl_staff_class', array('cid' => $curStaff['type']));
        }
    }

    include $this->template('staff_workflow');
} else if ($op == 'detail') {
    $curWorkflow = pdo_get('cwgl_workflow', array('id' => $_GPC['id']));
    if (!empty($curWorkflow)) {
        $step = $curWorkflow['step'];
    }
    $staffs = pdo_fetchall("select * from " . tablename('cwgl_staff_list') . " where weid=" . $_W['uniacid']);
    foreach ($staffs as $staff) {
        $staffsArray[$staff['id']] = $staff;
    }
    //当前部门
    $category = pdo_get('cwgl_staff_class', array('cid' => $curWorkflow['st1_staff_class']));
    include $this->template('staff_workflow_view');
} else if ($op == 'list') {

    $pindex = max(1, intval($_GPC['page']));
    $psize = empty($_GPC['psize']) ? 10 : intval($_GPC['psize']);

    $where = " where 1=1 ";
    $curStaff = pdo_get('cwgl_staff_list', array('uid' => $uid, 'weid' => $_W['uniacid']));
    if ($curStaff['name'] == '施妙青') {
        $where .= " and step in(4,5)";
    } else if ($curStaff['name'] == '施文雪') {

    } else {
        $where .= " and (st1_proposer_staff=" . $curStaff['id'] . " or st1_to1=" . $curStaff['id'] . " or st1_to2=" . $curStaff['id'] . ")";
    }
    $orderby = " order by st1_create_time desc";
    $limit = " limit " . ($pindex - 1) * $psize . ',' . $psize;

    $workflows = pdo_fetchall("select * from " . tablename('cwgl_workflow') . $where . $orderby . $limit);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('cwgl_workflow') . $where);

    for ($i = 0; $i < count($workflows); $i++) {
        if ($workflows[$i]['step'] == 2) {
            $workflows[$i]['curStaff'] = '施文雪';
            if ($curStaff['name'] == '施文雪') {
                $workflows[$i]['isEdit'] = true;
            }
        } else if ($workflows[$i]['step'] == 3) {
            $staffs = pdo_fetchall("select * from " . tablename('cwgl_staff_list') . " where weid=" . $_W['uniacid']);
            foreach ($staffs as $staff) {
                $staffsArray[$staff['id']] = $staff;
            }
            $workflows[$i]['curStaff'] = $staffsArray[$workflows[$i]['st1_to1']]['name'] . ',' . $staffsArray[$workflows[$i]['st1_to2']]['name'];
            if ($workflows[$i]['st1_to1'] == $curStaff['id'] || $workflows[$i]['st1_to2'] == $curStaff['id']) {
                $workflows[$i]['isEdit'] = true;
            }
        } else if ($workflows[$i]['step'] == 4) {
            $workflows[$i]['curStaff'] = '施妙青';
            if ($curStaff['name'] == '施妙青') {
                $workflows[$i]['isEdit'] = true;
            }
        }
    }
    include $this->template('staff_workflow_list');
}
